! function(window, document, $, undefined) {
    let init = function() {
        initEvent();
        renderList()
    }

    let initEvent = function() {
        $("#saveBtn").on("click", saveClick);
        $("tbody").on("click", "#delBtn", delClick)
        $("tbody").on("click", "#updateBtn", updateClick)
    }

    let saveClick = function() {

        let username = $("#username").val()
        let age = $("#age").val()
        let register = $("#register").val()
        let telephone = $("#telephone").val()
        let enData = $("#enData").val()
        let email = $("#email").val()
        let id = window.id;
        let url = ""
        let data = null;

        if ($(this).html() == "修改") {
            url = "/user/updateUser"
            data = {
                username,
                age,
                register,
                telephone,
                enData,
                email,
                id
            }

        } else {

            url = "/user/addUser"
            data = {
                username,
                age,
                register,
                telephone,
                enData,
                email
            }
        }



        $.ajax({
            type: "get",
            url,
            data,
            dataType: "json",
            success: function(response) {
                renderList()
            }
        });
    }
    let renderList = function() {
        let arr = [];



        $.ajax({
            type: "get",
            url: "/user/findUser",
            dataType: "json",
            success: function(response) {
                console.log(response)
                $.each(response, function(index, item) {
                    arr.push(`  <tr><td scope="col"><input type="checkbox" class="allCheck"></td>
                <td scope="col" class="username">${item.username}</td>
                <td scope="col" class="age">${item.age}</td>
                <td scope="col" class="register">${item.register}</td>
                <td scope="col" class="telephone">${item.telephone}</td>
                <td scope="col" class="emial">${item.email}</td>
                <td scope="col" class="enData">${item.enData}</td>
                <td scope="col"><button uid="${item._id}" id="delBtn">删除</button><button uid="${item._id}" id="updateBtn"  data-toggle="modal" data-target="#myModal">修改</button></td></tr>`)
                })
                $(".test").html(arr.join(""))
            }
        });
    }

    let delClick = function() {
        let id = $(this).attr("uid");
        if (confirm("确定删除吗？")) {
            $.ajax({
                type: "get",
                url: "/user/delUser",
                data: {
                    id: id
                },
                dataType: "json",
                success: function(response) {
                    renderList()
                }
            });
        }

    }

    let updateClick = function() {
        window.id = $(this).attr("uid");
        $("#username").val($(this).parent().siblings(".username").html())
        $("#age").val($(this).parent().siblings(".age").html())
        $("#register").val($(this).parent().siblings(".register").html())
        $("#telephone").val($(this).parent().siblings(".telephone").html())
        $("#email").val($(this).parent().siblings(".email").html())
        $("#enData").val($(this).parent().siblings(".enData").html())

        $("#saveBtn").html("修改")
        $(".modal-title").html("修改学生信息")


    }


    init();
}(window, document, jQuery)